package com.ltu.timkor6.gamesite.admin.client;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.ltu.timkor6.gamesite.shared.CompositeAbstract;
import com.ltu.timkor6.gamesite.shared.UserInfo;

public class CompositeUserList extends CompositeAbstract
{
	private Admin admin;
	
	private FlexTable table;
	
	public CompositeUserList(Admin admin)
	{
		this.admin = admin;
		
		panel.clear();
		
		// Title
		final Label title = new Label("Admin User List");
		title.setStyleName("panelHeader");
		panel.add(title);
		
		table = new FlexTable();
		table.setStyleName("admin-table");
		table.getRowFormatter().setStyleName(0, "header");
		table.getColumnFormatter().setStyleName(0, "first-column");
		table.getColumnFormatter().setStyleName(1, "second-column");
		table.getColumnFormatter().setStyleName(2, "third-column");
		table.getColumnFormatter().setStyleName(3, "fourth-column");
		panel.add(table);
		
		initWidget(panel);
	}

	public void updateComposite(List<UserInfo> list)
	{
		table.clear();
		
		int row = 0;
		
		table.setText(row, 0, "Username");
		table.setText(row, 1, "E-mail");
		table.setText(row, 2, "Test user");
		table.setText(row, 3, "Admin");
		
		if(list != null)
		{
			for(UserInfo userInfo : list)
			{
				row++;
				
				Anchor userName = new Anchor(userInfo.getUserName(), "#/admin/user/" + userInfo.getId());
				userName.setName("n" + userInfo.getId());
				userName.addClickHandler(new ClickHandler()
				{
					public void onClick(ClickEvent event)
					{
						Anchor userName = (Anchor) event.getSource();
						
						long userId = Long.parseLong(userName.getName().substring(1));
						
						admin.showUserInfo(userId);
					}
				});
				
				table.setWidget(row, 0, userName);
				table.setText(row, 1, userInfo.getEmail());
				table.setText(row, 2, String.valueOf(userInfo.isTestUser()));
				table.setText(row, 3, String.valueOf(userInfo.isAdmin()));
			}
		}
	}
}
